package com.emm.yixun.openapi.hander;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.app.LogoutReqDto;
import com.emm.yixun.common.app.LogoutResDto;
import com.emm.yixun.openapi.dto.base.BaseRequestDTO;
import com.emm.yixun.openapi.dto.base.BaseResponseDTO;
import com.emm.yixun.openapi.vo.LogoutReqVo;
import com.emm.yixun.system.service.app.UserServiceAppFacade;

/**
 * 退出登录接口
 * @author Administrator
 *
 */
@Service("logoutHander")
public class LogoutHander extends com.emm.yixun.openapi.hander.base.CommonAbstractHander{
	
	@Autowired
	private UserServiceAppFacade userServiceAppFacade;
	
	@Autowired
	private CacheManager cacheManager;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		LogoutReqVo req = (LogoutReqVo)reqDto;
		LogoutReqDto reqDto1 = req.getParams();
		BaseResponseDTO<LogoutResDto> res = new BaseResponseDTO<LogoutResDto>();
		res.setResData(userServiceAppFacade.logout(reqDto1));
		//密码修改成功后，清除该用户缓存token
		Cache  cache = cacheManager.getCache("activeSessionsCache");
		String userId_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userId";//获取userId缓存key
		String token_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userId"+reqDto1.getToken();//获取token缓存key
		String userCode_key = reqDto1.getMerchantId()+reqDto1.getUserId()+"userCode";//获取userCode缓存key
		Element element_userId = cache.get(userId_key);
		Element element_token = cache.get(token_key);
		Element element_userCode = cache.get(userCode_key);
		cache.removeElement(element_userId);
		cache.removeElement(element_token);
		cache.removeElement(element_userCode);
		return res;
	}
	
}
